﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ScreenMonitor.Common.Objects
{
    /// <summary>
    /// 为服务端接受客户端的连接请求后回复的消息所包含的内容提供封装
    /// </summary>
    public sealed class AcceptConnectObject
    {

        private static object StaticLockObj = new object();
        private object LockObj = new object();

        /// <summary>
        /// 表示服务端开启的Tcp端口
        /// </summary>
        public int ServerTcpPort { get; private set; }
        /// <summary>
        /// 表示发送的屏幕图片的高度
        /// </summary>
        public int ScreenViewImgHeight { get; private set; }
        /// <summary>
        /// 表示发送的屏幕图片的宽度
        /// </summary>
        public int ScreenViewImgWidth { get; private set; }
        /// <summary>
        /// 表示发送图片的类型
        /// </summary>
        public string ImageType { get; private set; }
        /// <summary>
        /// 表示发送图片的频率
        /// </summary>
        public int SendImgRate { get;private set; }
        /// <summary>
        /// 是否自动开始发送图片
        /// </summary>
        public bool IsAutoStartSendImg { get; private set; }
        /// <summary>
        /// 表示发送图片的缩放模式
        /// </summary>
        public PictureBoxSizeMode SendImgSizeMode { get; private set; }
        public AcceptConnectObject(int ServerTcpPort, int ScreenViewImgHeight, int ScreenViewImgWidth, string ImageType, int SendImgRate, bool IsAutoStartSendImg, PictureBoxSizeMode SendImgSizeMode)
        {
            this.ServerTcpPort = ServerTcpPort;
            this.ScreenViewImgHeight = ScreenViewImgHeight;
            this.ScreenViewImgWidth = ScreenViewImgWidth;
            this.ImageType = ImageType;
            this.SendImgRate = SendImgRate;
            this.IsAutoStartSendImg = IsAutoStartSendImg;
            this.SendImgSizeMode = SendImgSizeMode;
        }

        public byte[] ToByte()
        {
            lock (LockObj)
            {
                MemoryStream stream = new MemoryStream();
                BinaryWriter bw = new BinaryWriter(stream);
                bw.Write(this.ServerTcpPort);
                bw.Write(this.ScreenViewImgWidth);
                bw.Write(this.ScreenViewImgHeight);
                bw.Write(this.ImageType);
                bw.Write(this.SendImgRate);
                bw.Write(this.IsAutoStartSendImg);
                bw.Write((int)this.SendImgSizeMode);
                bw.Flush();
                byte[] r = stream.ToArray();
                return r;
            }
        }

        public static AcceptConnectObject FromByte(byte[] Data)
        {
            lock (StaticLockObj)
            {
                MemoryStream stream = new MemoryStream(Data);
                BinaryReader br = new BinaryReader(stream);
                int tcpp = br.ReadInt32();
                int svimgw = br.ReadInt32();
                int svimgh = br.ReadInt32();
                string imgt = br.ReadString();
                int simgrate = br.ReadInt32();
                bool isautosimg = br.ReadBoolean();
                PictureBoxSizeMode simgmode = (PictureBoxSizeMode)br.ReadInt32();
                AcceptConnectObject r = new AcceptConnectObject(tcpp, svimgh, svimgw,imgt,simgrate,isautosimg,simgmode) ;
                br.Close();
                return r;
            }
        }

    }
}
